<?php

class user_model extends CI_Model {

	function validate()
	{
		$this->db->where('user_name', $this->input->post('user_name'));
		$this->db->where('password', md5($this->input->post('password')));
		$query = $this->db->get('user');
		if($query->num_rows == 1)
		{
			return $query;
		}
		
	}
	
	function register()
	{
		if($this->input->post('sex')=='man')
		{
			$sex=0;
		}
		else 
		{
			$sex=1;
		}
		
		$city_id=0;
		//判断city表中是否有选择的城市
		$this->db->where('city_name', $this->input->post('curCity'));
		$query = $this->db->get('city');
		if($query->num_rows == 1){
			$result=$query->result();
			foreach($result as $row)
				$city_id=intval($row->id);
		}
		else{
			$city_data=array(
				'city_name'=>$this->input->post('curCity'),
				'city_type'=>intval($this->input->post('citytype')),
				'point_x'=>$this->input->post('point_x'),
				'point_y'=>$this->input->post('point_y')
			);
			$this->db->insert('city', $city_data);
			$city_id=$this->db->count_all('city');
			
		}
		
		$new_member_insert_data = array(
			'user_name' => $this->input->post('user_name'),
			'email' => $this->input->post('email'),			
			'password' => md5($this->input->post('password')),
			'sex' => $sex,
			'city_id'=>$city_id							
		);
		
		$insert = $this->db->insert('user', $new_member_insert_data);
		$query_id=$this->db->insert_id();
		
		
		
		$data=array('user_id'=>$query_id);
		$query_setting=$this->db->insert('user_setting',$data);
		return $insert && $query_setting;
	}
	
	function get_user_city()
	{
		$user_id=$this->session->userdata('user_id');
		$this->db->where('user_id',$user_id);
		$query = $this->db->get('user');
		$city_id=$query->row()->city_id;
		return $city_id;
		#$query = $this->db->get_where('location', array('location_id' => $location_id));		
	}
	
	function get_user_info($user_id)
	{
		$this->db->where('user_id',$user_id);
		$query = $this->db->get('user');
		if($query->num_rows == 1)
		{
			return $query->row();
		}
	}
}